#!/bin/csh

#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# FERRET MAPS OF TROPICAL STORM INITIAL POSITION
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

#=====================================================================
# --- INPUT
#=====================================================================

if ( $#argv == 0 ) then
#-------------------------------------------------
echo    "     "
echo -n "           INPUT DIRECTORY  = " ; set adir     = `echo $<`
echo -n "           OUTPUT DIRECTORY = " ; set odir     = `echo $<`
echo -n "          FIRST YEAR [1979] = " ; set yrbeg    = `echo $< `
echo -n "           LAST YEAR [1979] = " ; set yrend    = `echo $< `
echo -n "                EXPERIMENTS = " ; set exp_list = `echo $<`
echo    "     "
if ( $#yrbeg == 0 ) set yrbeg = 1979
if ( $#yrend == 0 ) set yrend = 1979
echo    "     "
#-------------------------------------------------
else
#-------------------------------------------------
set adir     = $1 ; shift
set odir     = $1 ; shift
set yrbeg    = $1 ; shift
set yrend    = $1 ; shift
set exp_list = `echo $argv`
#-------------------------------------------------
endif

#=====================================================================
# --- PATHNAMES & DIRECTORIES
#=====================================================================

# --- input  directory
if ( $#adir == 0 ) set adir = /home/jjs/tstorms_wehner/sample_output

# --- output directory
if ( $#odir   == 0 ) set odir = /home/jjs/tstorms_wehner/sample_output
if ( ! -e $odir ) mkdir -p $odir
cd $odir
  
#--- ferret stuff
set fdir = /home/jjs/tstorms_wehner/scripts/my_ferret_go

#=====================================================================
# --- GET DATA FOR PLOTTING
#=====================================================================

set xargs = dummy

                            set tyear = ${yrbeg}
if ( ${yrbeg} != ${yrend} ) set tyear = ${yrbeg}-${yrend}

foreach exp ( $exp_list )
#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
set expx = `echo $exp | tr "_" "-"`

       @ year  = $yrbeg
while ( $year <= $yrend)
     set fin  =  ${adir}/${exp}/ori_${year}
if ( -e $fin  ) then
    cat $fin >> fin.${expx}.${tyear}
endif
@ year ++
end

set nts = `wc -l fin.${expx}.${tyear}`

set xargs = ($xargs ${expx}.${tyear})

echo  ${expx}.${tyear} $nts[1]
#zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
end
echo "    "

shift xargs

#=====================================================================
# --- GET & SET JNL FILES
#=====================================================================

set nexp = $#exp_list

cp ${fdir}/domain_40ns.jnl         domain.jnl
cp ${fdir}/nlots_${nexp}_40ns.jnl  nlots.jnl
cp ${fdir}/ts_ori.jnl              ts_ori.jnl


cat << EOF > fscript.jnl
\cancel mode verify
set mode metafile
go nlots.jnl
EOF

@ i = 0
foreach exp ( $xargs )
@ i ++
if ( $nexp > 1 ) then
echo "set viewport v${i}" >> fscript.jnl
endif
echo "go ts_ori.jnl $exp" >> fscript.jnl
end

echo "quit" >> fscript.jnl

#=====================================================================
# --- PLOT MAP
#=====================================================================

set pict = map_ori

ferret << EOF
go fscript.jnl
quit
EOF

/usr/local/ferret/bin/gksm2ps -l cps -o ${pict}.ps metafile.plt

mgv -noantialias ${pict}.ps
            gzip ${pict}.ps

#=====================================================================
# --- CLEAN UP
#=====================================================================

foreach exp ( $exp_list )
  set expx = `echo $exp | tr "_" "-"`
  rm -f fin.${expx}.${tyear}
end

rm  -f  ferret.jnl*
rm  -f *.plt
rm  -f *.jnl

echo "   "
echo " *** fmaps done *** "
echo "   "

#=====================================================================
exit
